package server.gameplay;

import model.MultiPath;

/**
 * A SendReq is a post that keeps track of how many units is queued at one path.
 * Is asked to send a set of units, but only do so every 400th millisecond.
 */
public class SendReq {
	private MultiPath path;
	private ServerPlayerPackage owner;
	private int amount;
	private long timer;
	private Building building;
	private Building targetBuilding;

	public SendReq(MultiPath p, ServerPlayerPackage player, Building b, int a,
			Building targetBuilding) {
		path = p;
		owner = player;
		amount = a;
		building = b;
		timer = System.currentTimeMillis() - 1300;
		this.targetBuilding = targetBuilding;
	}

	public int grantReq() {
		if (System.currentTimeMillis() - timer >= 1500) {
			timer = System.currentTimeMillis();
			if (amount > 8) {
				amount -= 8;
				return 8;
			} else {
				int tmp = amount;
				amount = 0;
				return tmp;
			}
		} else
			return 0;

	}

	public boolean moreReq() {
		return (amount != 0);
	}

	public boolean sameReq(MultiPath path2, int amount) {
		if (path2 != null && path2.equals(path)) {
			if (amount > this.amount)
				this.amount = amount;
			return true;
		}
		return false;
	}

	public ServerPlayerPackage getOwner() {
		return owner;
	}

	public MultiPath getPath() {
		return path;
	}

	public Building getBuilding() {
		return building;
	}

	public void clear() {
		amount = 0;
	}

	public Building getTarget() {
		return targetBuilding;
	}
}
